﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dotnet.FormExtensions
{
    partial class Ext
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="textbox"></param>
        /// <param name="txt"></param>
        /// <param name="isTime"></param>
        public static void AppendTextAsync(this RichTextBox textbox, string txt, bool isTime = true)
        {
            if (textbox.InvokeRequired)
            {
                var InvokeCallback = new Action<RichTextBox, string, bool>(AppendTextAsync);
                textbox.Invoke(InvokeCallback, textbox, txt, isTime);
            }
            else
            {
                if (isTime)
                {
                    textbox.AppendText($"【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}】{txt}\r\n");
                }
                else
                {
                    textbox.AppendText(txt + "\r\n");
                }
            }
        }

        /// <summary>
        /// 设置字符串颜色
        /// </summary>
        /// <param name="richTxt"></param>
        /// <param name="txt">待设置的字符串</param>
        /// <param name="color">设置颜色</param>
        public static void SetStringColor(this RichTextBox richTxt,string txt,Color color)
        {
            int pos = richTxt.Find(txt);
            if (pos < 0)
                return;
            return;
             //SetStringColor(richTxt, txt, pos, txt.Length, color);
            //richTxt.SelectionStart = richTxt.Find(txt);
            //richTxt.SelectionLength = txt.Length;
            //richTxt.SelectionFont = new Font("宋体", 11F, FontStyle.Bold|FontStyle.Italic);//| FontStyle.Italic
            //richTxt.SelectionColor = color;
        }
        /// <summary>
        /// 设置从指定位置起的颜色字符串颜色
        /// </summary>
        /// <param name="richTxt"></param>
        /// <param name="txt">待设置的字符串</param>
        /// <param name="startPos">字段开始位置</param>
        /// <param name="len">字段长度</param>
        /// <param name="color">设置颜色</param>
        public static void SetStringColor(this RichTextBox richTxt, string txt, int startPos,Color color)
        {
            if (startPos < 0)
            {
                startPos = richTxt.Find(txt);
            }
            if (startPos < 0)
                return;
            richTxt.SelectionStart = startPos;
            richTxt.SelectionLength = txt.Length;
            richTxt.SelectionFont = new Font("宋体", 12F, FontStyle.Bold); //| FontStyle.Italic
            richTxt.SelectionColor = color;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="richTxt"></param>
        /// <param name="txt"></param>
        /// <param name="startPos"></param>
        /// <param name="len"></param>
        /// <param name="color"></param>
        public static void SetStringColor(this RichTextBox richTxt, string txt, int startPos,int len, Color color)
        {
            if (startPos < 0)
            {
                startPos = richTxt.Find(txt);
            }
            if (startPos < 0)
                return;
            richTxt.SelectionStart = startPos;
            richTxt.SelectionLength = len;
            richTxt.SelectionFont = new Font("宋体", 12F, FontStyle.Bold);
            richTxt.SelectionColor = color;
        }
        public static void SetStringColor(this RichTextBox richTxt, string txt, int startPos, int len, Color color,Font font)
        {
            if (startPos < 0)
            {
                startPos = richTxt.Find(txt);
            }
            if (startPos < 0)
                return;
            richTxt.SelectionStart = startPos;
            richTxt.SelectionLength = len;
            richTxt.SelectionFont = new Font("宋体", 11F, FontStyle.Bold | FontStyle.Italic);
            richTxt.SelectionColor = color;
        }
    }
}
